import { useDictStore } from '@/stores/dict';
import { getDicts, IDictItemVO } from '@/api/admin/dict';
import { ref, toRefs } from 'vue';

/**
 * 获取字典数据
 */
export function useDict(...args: string[]): any {
	const res = ref<Record<string, IDictItemVO[]>>({});
	const dictStore = useDictStore();

	args.forEach((dictType) => {
		res.value[dictType] = [];
		const dicts = dictStore.getDict(dictType);
		if (dicts) {
			res.value[dictType] = dicts;
		} else {
			getDicts(dictType).then((resp) => {
				res.value[dictType] = resp.data.sort((a, b) => a.sortOrder - b.sortOrder);
				dictStore.setDict(dictType, res.value[dictType]);
			});
		}
	});

	return toRefs(res.value);
}
